package com.cskaoyan.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.mall.product.converter.dto.PlatformAttributeInfoConverter;
import com.cskaoyan.mall.product.converter.param.PlatformAttributeInfoParamConverter;
import com.cskaoyan.mall.product.dto.PlatformAttributeInfoDTO;
import com.cskaoyan.mall.product.dto.PlatformAttributeValueDTO;
import com.cskaoyan.mall.product.mapper.PlatformAttrInfoMapper;
import com.cskaoyan.mall.product.mapper.PlatformAttrValueMapper;
import com.cskaoyan.mall.product.model.PlatformAttributeInfo;
import com.cskaoyan.mall.product.model.PlatformAttributeValue;
import com.cskaoyan.mall.product.query.PlatformAttributeParam;
import com.cskaoyan.mall.product.service.PlatformAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author Pudding
 * @Date 2024/6/29 16:08
 */
@Service
public class PlatformAttributeServiceImpl implements PlatformAttributeService {

    @Autowired
    PlatformAttrInfoMapper platformAttrInfoMapper;
    @Autowired
    PlatformAttrValueMapper platformAttrValueMapper;
    @Autowired
    PlatformAttributeInfoConverter platformAttributeInfoConverter;
    @Autowired
    PlatformAttributeInfoParamConverter platformAttributeInfoParamConverter;



    //1. 根据一级分类id，二级分类id，三级分类id查询平台属性 和 它对应的平台属性值。
    @Override
    public List<PlatformAttributeInfoDTO> getPlatformAttrInfoList(Long firstLevelCategoryId, Long secondLevelCategoryId, Long thirdLevelCategoryId) {

        //1.1 通过自定义的mybatis查询来关联查询
        List<PlatformAttributeInfo> platformAttributeInfos = platformAttrInfoMapper.selectPlatFormAttrInfoList(firstLevelCategoryId, secondLevelCategoryId, thirdLevelCategoryId);

        //1.2 将PO对象转换为DTO对象
        List<PlatformAttributeInfoDTO> platformAttributeInfoDTOS = platformAttributeInfoConverter.platformAttributeInfoPOs2DTOs(platformAttributeInfos);

        return platformAttributeInfoDTOS;
    }

    @Override
    public void savePlatformAttrInfo(PlatformAttributeParam platformAttributeParam) {

        //1. 转化请求参数，转为PO对象，为了插入数据库
        PlatformAttributeInfo platformAttributeInfo = platformAttributeInfoParamConverter.attributeInfoParam2Info(platformAttributeParam);

        //2. 如何区分保存与修改呢？
        // 如果是修改 platformAttributeInfo.getId() != null
        // 如果是保存 platformAttributeInfo.getId == null
        //下面是对表platform_attr_info操作
        if (platformAttributeInfo.getId() != null){
            // 修改平台属性信息表
            platformAttrInfoMapper.updateById(platformAttributeInfo);
        }else {
            // 保存平台属性到信息表
            platformAttrInfoMapper.insert(platformAttributeInfo);
        }

        //3. 保存平台属性值、修改平台属性值（先删除原来的平台属性值platformAttrValue，在添加新值）
        //下面是对表platform_attr_value操作
        LambdaQueryWrapper<PlatformAttributeValue> queryWrapper = new LambdaQueryWrapper<>();
        //3.1 先删除平台属性原本在数据库中对应的属性值
        queryWrapper.eq(PlatformAttributeValue::getAttrId, platformAttributeInfo.getId());
        platformAttrValueMapper.delete(queryWrapper);

        //3.2 获取页面传递过来的所有平台属性值数据List，这里的List中只有属性值的名称
        List<PlatformAttributeValue> attrValueList = platformAttributeInfo.getAttrValueList();
        if (!CollectionUtils.isEmpty(attrValueList)){
            //循环遍历平台属性值List数据
            for (PlatformAttributeValue platformAttributeValue : attrValueList) {
                platformAttributeValue.setId(null);
                //获取平台属性id，放到表platform_attr_value中的attr_id字段
                platformAttributeValue.setAttrId(platformAttributeInfo.getId());
                //将该条平台属性值信息插入到表中
                platformAttrValueMapper.insert(platformAttributeValue);
            }
        }
    }

    @Override
    public List<PlatformAttributeValueDTO> getPlatformAttrValueList(Long attrId) {

        //1. 构造查询条件
        LambdaQueryWrapper<PlatformAttributeValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlatformAttributeValue::getAttrId, attrId);

        //2. 进行查询
        List<PlatformAttributeValue> platformAttributeValues = platformAttrValueMapper.selectList(queryWrapper);

        //3. 将PO对象转换为DTO对象
        List<PlatformAttributeValueDTO> platformAttributeValueDTOS = platformAttributeInfoConverter.platformAttributeValuePOs2DTOs(platformAttributeValues);

        return platformAttributeValueDTOS;
    }
}
